* Begin log file
capture log close sublog
log using "$projdir/log/4_qwi_stack.txt", name(sublog) text replace

*-------------------------------------------------------------------------------
* Description: Stack and average the uncorrected, and bias-corrected, and 
* pandemic-corrected synthetic control results for various groupings of interest
* for all ages and genders for QWI data
* Author: Justin Wiltshire and Denis Sosinskiy
*
* Updated: January, 2024
*-------------------------------------------------------------------------------
clear

tempfile core
tempfile core0

* All treated counties
foreach y in sepr sepnewr {	
	local n = 7225
	local s = "allage" 
		
		* Ensure necessary folders exists
		capture mkdir "$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/agg_att"
		capture mkdir "$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/agg_att/bias_corrected"
		capture mkdir "$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/agg_att/covid_corrected"
		
		* Add trunit and trperiod to the uncorrected individual estimates if needed
		foreach z in California NY {
		
			* Set trperiod macro
			local _trp = 218
			if "`z'" == "NY" {
				local _trp = 216
			}
		
			* Get filelist
			local filelist: dir "$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/`z'" files "*.dta"
			
			* Add the variables if needed
			foreach f of local filelist {
				if strpos("`f'", "_ate") == 0 {
					qui use "$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/`z'/`f'", clear
					
					* Extract and create trunit
					capture assert trunit
					if _rc != 0 {
						local _trunit = subinstr("`f'", "tr_", "", .)
						local _trunit = subinstr("`_trunit'", ".dta", "", .)
						qui gen trunit = `_trunit'
					}
																		
					* Exclude mid counties in NY
					if !inlist(`_trunit', 36007, 36027, 36063, 36065, 36069, 36083, 36085, 36087, 36091, 36093, 36111) {
						local inclunits "`inclunits' `_trunit'"
					}
					
					* Generate trperiod
					capture assert trperiod
					if _rc != 0 {
						qui gen trperiod = `_trp'
					}
					
					* Add pop10 variable if needed
					capture assert pop10
					if _rc != 0 & !inlist(`_trunit', 36007, 36027, 36063, 36065, 36069, 36083, 36085, 36087, 36091, 36093, 36111) {
						save `core', replace
						use "$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/`z'/corrected/`f'", clear
						keep cty_fips pop10
						collapse (mean) pop10, by(cty_fips)
						save `core0', replace
						use `core', clear
						merge m:1 cty_fips using `core0', nogen keep(1 3)
					}
					
					* Save the file
					qui save "$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/`z'/`f'", replace
				}
			}
		}

		* Uncorrected estimates
		#delimit ;
			stackscpvals, 
				gap(gap) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-17) emax(31) 
				filepath("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/California") 
				filepath_2("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/NY")
				savepath("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/agg_att")
				savename("att");
			#delimit cr
		
		* Bias-corrected estimates
		#delimit ;
			stackscpvals, 
				gap(gap_bc) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-17) emax(31) 
				filepath("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/California") 
				filepath_2("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/NY")
				savepath("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/agg_att/bias_corrected")
				savename("bias_corr_att");
			#delimit cr
		
		* Covid-corrected estimates
		#delimit ;
			stackscpvals, 
				gap(p_gap) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-17) emax(31) 
				filepath("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/California/corrected") 
				filepath_2("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/NY/corrected")
				savepath("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/agg_att/covid_corrected")
				savename("covid_corr_att");
			#delimit cr
}

* Excluding treated counties with local minimum wages
foreach y in sepr sepnewr {	
	local n = 7225
	local s = "allage" 
		
		* Ensure necessary folders exists
		capture mkdir "$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/nolocal_att"
		capture mkdir "$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/nolocal_att/bias_corrected"
		capture mkdir "$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/nolocal_att/covid_corrected"
		
		* Generate trunit and trperiod variables if needed, and build inclunits list
		local inclunits
		foreach z in California NY {
		
			* Set trperiod macro
			local _trp = 218
			if "`z'" == "NY" {
				local _trp = 216
			}
		
			* Get filelist
			local filelist: dir "$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/`z'" files "*.dta"
			
			* Add the variables if needed, and add the appropriate counties to the macro
			foreach f of local filelist {
				if strpos("`f'", "_ate") == 0 {
					qui use "$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/`z'/`f'", clear
					
					* Extract and create trunit (if necessary) and add to inclunits list
					local _trunit = subinstr("`f'", "tr_", "", .)
					local _trunit = subinstr("`_trunit'", ".dta", "", .)
					capture assert trunit
					if _rc != 0 {
						qui gen trunit = `_trunit'
					}
					* Exclude counties with local minimum wages & mid counties in NY
					if !inlist(`_trunit', 6001, 6013, 6037, 6041, 6073, 6075, 6081, 6085, 6097, 36005, 36047, 36061, 36081, 36085, 36007, 36027, 36063, 36065, 36069, 36083, 36085, 36087, 36091, 36093, 36111) {
						local inclunits "`inclunits' `_trunit'"
					}
	
					* Generate trperiod
					capture assert trperiod
					if _rc != 0 {
						qui gen trperiod = `_trp'
					}
					
					* Add pop10 variable if needed
					capture assert pop10
					if _rc != 0 & !inlist(`_trunit', 36007, 36027, 36063, 36065, 36069, 36083, 36085, 36087, 36091, 36093, 36111) {
						save `core', replace
						use "$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/`z'/corrected/`f'", clear
						keep cty_fips pop10
						collapse (mean) pop10, by(cty_fips)
						save `core0', replace
						use `core', clear
						merge m:1 cty_fips using `core0', nogen keep(1 3)
					}
					
					* Save the file
					qui save "$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/`z'/`f'", replace
				}
			}
		}
		
		* Uncorrected estimates
		#delimit ;
			stackscpvals, 
				gap(gap) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-17) emax(31) 
				filepath("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/California") 
				filepath_2("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/NY")
				savepath("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/nolocal_att")
				savename("att")
				keeptrunits(`inclunits');
			#delimit cr
		
		* Bias-corrected estimates
		#delimit ;
			stackscpvals, 
				gap(gap_bc) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-17) emax(31) 
				filepath("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/California") 
				filepath_2("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/NY")
				savepath("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/nolocal_att/bias_corrected")
				savename("bias_corr_att")
				keeptrunits(`inclunits');
			#delimit cr
		
		* Covid-corrected estimates
		#delimit ;
			stackscpvals, 
				gap(p_gap) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-17) emax(31) 
				filepath("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/California/corrected") 
				filepath_2("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/NY/corrected")
				savepath("$projdir/dta/analysis/qwi/`y'/naics`n'/`s'/nolocal_att/covid_corrected")
				savename("covid_corr_att")
				keeptrunits(`inclunits');
			#delimit cr
}

* Close the log
log close sublog
